﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public Class ObjektEinheitenmenue
    Dim BreiteeinesSymbols As Integer
    Dim Stelle As Integer = -1
    Shared Cursor As Texture
    Shared Texturen() As Texture = {}
    Dim Namen() As String = {}
    Public Tags() As String = {}
    Dim Symbole() As Texture = {}
    Dim SymbolMasken() As Texture = {}

    Public Enabled() As Boolean = {}
    Dim MenueRechteckGross As Rectangle
    Dim Text As ObjektText
    Private Frames As Integer
    Public Property Frame() As Integer
        Get
            Return Frames
        End Get
        Set(ByVal value As Integer)
            Frames += 1
            If Frames > 3 Then
                Frames = 0
            End If
        End Set
    End Property
    Dim Bildframes As Boolean = False
    Public Cursorposition As Point = New Point
    Dim MitDispose As Boolean = False
    ''' <summary>
    ''' Zahl der Items pro Spalte.
    ''' </summary>
    ''' <remarks></remarks>
    Dim ItemZahl As Integer = 0
    Dim Spaltenzahl As Integer = 0
    Enum Modi
        Einheitbauen
        Gebaeudebauen
        Anderes
    End Enum
    Dim Modus As Modi
    Public Feldpunkt As Point
    Public Function GetModus() As Modi
        Return Modus
    End Function
    Public Sub New(ByVal neuNamen() As String, ByVal neuSymbole() As Texture, ByVal Spieler As Integer, ByVal neuBreiteeinesSymbols As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal neuBildframes As Boolean, ByVal neuEnabled() As Boolean, ByVal SpielmodusBeibehalten As Boolean, ByVal MitDispose As Boolean, ByVal Modus As Modi, Optional ByVal SymbolMasken As Texture() = Nothing)
        ReDim Tags(neuNamen.Length - 1)
        ReDim Symbole(neuSymbole.Length - 1)
        For I = 0 To Tags.Length - 1
            Tags(Tags.Length - I - 1) = neuNamen(I)
            Symbole(Symbole.Length - I - 1) = neuSymbole(I)
        Next
        If IsNothing(neuEnabled) = False Then
            ReDim Enabled(neuEnabled.Length - 1)
            For I = 0 To Enabled.Length - 1
                Enabled(Enabled.Length - I - 1) = neuEnabled(I)
            Next
        End If
        If SymbolMasken Is Nothing Then
            ReDim Me.SymbolMasken(-1)
        Else
            ReDim Me.SymbolMasken(SymbolMasken.Length - 1)
            For I = 0 To Me.SymbolMasken.Length - 1
                Me.SymbolMasken(Me.SymbolMasken.Length - I - 1) = SymbolMasken(I)
            Next
        End If

        Feldpunkt = Hauptmenue.SpielfeldPunkt
        ItemZahl = 15
        If DataSettings.BildschirmBreite < 400 Then ItemZahl = 20
        Dim R As Integer = 0
        Spaltenzahl = Math.DivRem(neuNamen.Length, ItemZahl, R)
        If R > 0 Then Spaltenzahl += 1
        ItemZahl = Math.DivRem(neuNamen.Length, Spaltenzahl, R)
        If R > 0 Then ItemZahl += 1
        Objektverwalter.MarkierteFelder.Clear()
        If SpielmodusBeibehalten = False Then
            Dim M As ObjektSpielMenue = Objektverwalter.Menue
            M.Spielmodus = ObjektSpielMenue.Spielmodi.AuswahlmenueEinheit
        End If
        Me.Modus = Modus
        Me.MitDispose = MitDispose
        Array.Resize(Namen, Tags.Length)
        For I = 0 To Namen.Length - 1
            Dim T1 As String() = Tags(I).Split(" ")
            Dim EName As String = ""
            If Char.IsNumber(T1(T1.Length - 1).ToCharArray()(0)) Then
                For I2 = 0 To T1.Length - 2
                    If I2 < T1.Length - 2 Then
                        EName += T1(I2) + " "
                    Else
                        EName += T1(I2)
                    End If
                Next
                Namen(I) = Sprachconverter.Translate(EName) + " " + T1(T1.Length - 1)
            Else
                Namen(I) = Sprachconverter.Translate(Tags(I))
            End If
        Next


        Bildframes = neuBildframes
        BreiteeinesSymbols = neuBreiteeinesSymbols
        LoadCursor()

        Array.Resize(Texturen, 3)
        Texturen(0) = Grafikengine.LoadTexture(Image.FromFile(DataSettings.Resourcenpfad + "Menu Icons\MenueobererRand.PNG"), Hauptmenue.Farbtabelleerstellen(Objektverwalter.Spielfeld.AktuellerSpieler.Farbe))
        Texturen(1) = Grafikengine.LoadTexture(Image.FromFile(DataSettings.Resourcenpfad + "Menu Icons\MenueMitte.PNG"), Hauptmenue.Farbtabelleerstellen(Objektverwalter.Spielfeld.AktuellerSpieler.Farbe))
        Texturen(2) = Grafikengine.LoadTexture(Image.FromFile(DataSettings.Resourcenpfad + "Menu Icons\MenueuntererRand.PNG"), Hauptmenue.Farbtabelleerstellen(Objektverwalter.Spielfeld.AktuellerSpieler.Farbe))
        MenueRechteckGross.Height = 13 + Hauptmenue.Feldbreite * ItemZahl
        If MenueRechteckGross.Height + Y + 15 > DataSettings.BildschirmHoehe Then
            MenueRechteckGross.Y = DataSettings.BildschirmHoehe - MenueRechteckGross.Height - 15
        Else
            MenueRechteckGross.Y = Y
        End If
        Dim Breite As Rectangle
        Dim Farben() As Color = {Color.Black}
        If Hauptmenue.ZoomedIn Then
            Text = New ObjektText(Nothing, 0, 0, Farben, Schriftengine.Schriftarten.Arial, 12, FontStyle.Regular)
        Else
            Text = New ObjektText(Nothing, 0, 0, Farben, Schriftengine.Schriftarten.Arial, 8, FontStyle.Regular)
        End If
        Grafikengine.Sprite.Transform = Matrix.Identity
        For I = 0 To Namen.Length - 1
            Breite = Text.GetSchriftart.MeasureString(Grafikengine.Sprite, Namen(I), DrawTextFormat.None, Color.Black)
            If MenueRechteckGross.Width < Breite.Width Then
                MenueRechteckGross.Width = Breite.Width
            End If
        Next
        MenueRechteckGross.Width += 10 + BreiteeinesSymbols
        If MenueRechteckGross.Width < 61 Then
            MenueRechteckGross.Width = 61
        End If
        If MenueRechteckGross.Width * Spaltenzahl + X + 10 > DataSettings.BildschirmBreite Then
            MenueRechteckGross.X = DataSettings.BildschirmBreite - MenueRechteckGross.Width * Spaltenzahl - 10
        Else
            MenueRechteckGross.X = X
        End If
        'Jetzt wird der TExt erschaffen
        ReDim Farben(-1)
        If Enabled.Length > 0 Then
            Array.Resize(Farben, Enabled.Length)
            For I = 0 To Farben.Length - 1
                If Enabled(I) = False Then
                    Farben(I) = Color.Black
                Else
                    Farben(I) = Color.Red
                End If
            Next
        Else
            ReDim Farben(0)
            Farben(0) = Color.Black
        End If

        Cursorposition = New Point
        CursorpositionVeraendern(-1, 0)

        MusikEngine.LoadWav(DataSettings.Resourcenpfad + "Sound\Sound Effects\Overworld\Einheitausgewaehlt.wav", False)
        ObjektMaus.Visible = False
    End Sub
    Private Shared Sub LoadCursor()
        If Cursor Is Nothing Then
            If Hauptmenue.ZoomedIn Then
                Cursor = Grafikengine.LoadTexture(DataSettings.Resourcenpfad + "Cursor\Zoomed In\ZeigerMenue.png")
            Else
                Cursor = Grafikengine.LoadTexture(DataSettings.Resourcenpfad + "Cursor\Zoomed Out\ZeigerMenue.png")
            End If
        End If
    End Sub
    Public Function Malen() As Boolean
        'Grafikengine.Grafikkarte.EndScene()
        'Grafikengine.Grafikkarte.BeginScene()
        'Welchen Teil des Bildes wir malen wollen
        Dim sngRotation As Double = 0
        Dim intColor As Color = Color.FromArgb(255, 255, 255, 255)
        With Grafikengine.ptfRotCenter
            .X = 0
            .Y = 0
        End With
        'Oberen Teil malen
        For I2 = 0 To Spaltenzahl - 1
            Grafikengine.ptfPosition = New Drawing.Point(MenueRechteckGross.X + MenueRechteckGross.Width * I2, MenueRechteckGross.Y)
            For I = 0 To 2
                Select Case I
                    Case 0
                        With Grafikengine.recBild
                            .X = 0 : .Width = 3
                            .Y = 0 : .Height = 5
                        End With
                        With Grafikengine.szfScale
                            .Height = 5
                            .Width = 3
                        End With
                    Case 1
                        Grafikengine.ptfPosition.X += 3
                        With Grafikengine.recBild
                            .X = 3 : .Width = 54
                            .Y = 0 : .Height = 5
                        End With
                        With Grafikengine.szfScale
                            .Height = 5
                            .Width = MenueRechteckGross.Width - 7
                        End With
                    Case 2
                        Grafikengine.ptfPosition.X += MenueRechteckGross.Width - 7
                        With Grafikengine.recBild
                            .X = 57 : .Width = 4
                            .Y = 0 : .Height = 5
                        End With
                        With Grafikengine.szfScale
                            .Height = 5
                            .Width = 4
                        End With
                End Select
                Grafikengine.Sprite.Draw2D(Texturen(0), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            Next
        Next
        'Mitte malen
        For I3 = 0 To Spaltenzahl - 1
            For I = 0 To 2
                Grafikengine.ptfPosition = New Drawing.Point(MenueRechteckGross.X + I3 * MenueRechteckGross.Width, MenueRechteckGross.Y)
                Select Case I
                    Case 0
                        With Grafikengine.recBild
                            .X = 0 : .Width = 3
                            .Y = 0 : .Height = Hauptmenue.Feldbreite
                        End With
                        With Grafikengine.szfScale
                            .Height = Hauptmenue.Feldbreite
                            .Width = 3
                        End With
                    Case 1
                        Grafikengine.ptfPosition.X += 3
                        With Grafikengine.recBild
                            .X = 3 : .Width = 54
                            .Y = 0 : .Height = Hauptmenue.Feldbreite
                        End With
                        With Grafikengine.szfScale
                            .Height = Hauptmenue.Feldbreite
                            .Width = MenueRechteckGross.Width - 7
                        End With
                    Case 2
                        Grafikengine.ptfPosition.X += MenueRechteckGross.Width - 4
                        With Grafikengine.recBild
                            .X = 57 : .Width = 4
                            .Y = 0 : .Height = Hauptmenue.Feldbreite
                        End With
                        With Grafikengine.szfScale
                            .Height = Hauptmenue.Feldbreite
                            .Width = 4
                        End With
                End Select
                Grafikengine.ptfPosition.Y += 5

                Dim Max As Integer = (I3 + 1) * ItemZahl - 1
                If Max > Tags.Length - 1 Then Max = Tags.Length - 1
                For I2 = I3 * ItemZahl To Max
                    Grafikengine.Sprite.Draw2D(Texturen(1), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
                    Grafikengine.ptfPosition.Y += Hauptmenue.Feldbreite
                Next
            Next
        Next
        'unten
        For I2 = 0 To Spaltenzahl - 1
            Dim Max As Integer = (I2 + 1) * ItemZahl - 1
            Dim Min As Integer = I2 * ItemZahl
            If Max > Tags.Length - 1 Then Max = Tags.Length - 1
            Grafikengine.ptfPosition = New Point(MenueRechteckGross.X + MenueRechteckGross.Width * I2, (Max - Min + 1) * Hauptmenue.Feldbreite + 5 + MenueRechteckGross.Y)
            For I = 0 To 2
                Select Case I
                    Case 0
                        With Grafikengine.recBild
                            .X = 0 : .Width = 3
                            .Y = 0 : .Height = Hauptmenue.Feldbreite / 2
                        End With
                        With Grafikengine.szfScale
                            .Height = Hauptmenue.Feldbreite / 2
                            .Width = 3
                        End With
                    Case 1
                        Grafikengine.ptfPosition.X += 3
                        With Grafikengine.recBild
                            .X = 3 : .Width = 54
                            .Y = 0 : .Height = Hauptmenue.Feldbreite / 2
                        End With
                        With Grafikengine.szfScale
                            .Height = Hauptmenue.Feldbreite / 2
                            .Width = MenueRechteckGross.Width - 7
                        End With
                    Case 2
                        Grafikengine.ptfPosition.X += MenueRechteckGross.Width - 7
                        With Grafikengine.recBild
                            .X = 57 : .Width = 4
                            .Y = 0 : .Height = Hauptmenue.Feldbreite / 2
                        End With
                        With Grafikengine.szfScale
                            .Height = Hauptmenue.Feldbreite / 2
                            .Width = 4
                        End With
                End Select
                Grafikengine.Sprite.Draw2D(Texturen(2), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
            Next
        Next
        'Text und Symbole malen


        If Bildframes = True Then
            Grafikengine.recBild.X = Frame * BreiteeinesSymbols
        End If
        Grafikengine.szfScale = New Size(Hauptmenue.Feldbreite, Hauptmenue.Feldbreite)

        Dim Mitte As Integer = (Hauptmenue.Feldbreite - BreiteeinesSymbols) / 2
        For I2 = 0 To Spaltenzahl - 1
            Grafikengine.ptfPosition = New Drawing.Point(MenueRechteckGross.X + 4 + MenueRechteckGross.Width * I2, MenueRechteckGross.Y + 5 + Mitte)
            Dim Max As Integer = (I2 + 1) * ItemZahl - 1
            If Max > Tags.Length - 1 Then Max = Tags.Length - 1
            Dim Min As Integer = I2 * ItemZahl
            Dim E(Max - Min) As Color
            Dim S(Max - Min) As String
            For I = Min To Max
                S(I - Min) = Namen(I)
                If Enabled.Length > 0 Then
                    If Enabled(I) = True Then
                        intColor = Color.FromArgb(255, 180, 180, 180)
                        E(I - Min) = Color.Red
                    Else
                        E(I - Min) = Color.Black
                    End If
                Else
                    E(I - Min) = Color.Black
                End If



                If I < SymbolMasken.Length Then
                    If SymbolMasken(I) IsNot Nothing Then
                        If Bildframes Then
                            With Grafikengine.recBild
                                .X = Frame * Hauptmenue.Feldbreite : .Width = Hauptmenue.Feldbreite
                                .Y = 0 : .Height = Hauptmenue.Feldbreite
                            End With
                        Else
                            With Grafikengine.recBild
                                .X = 0 : .Width = Hauptmenue.Feldbreite
                                .Y = 0 : .Height = Hauptmenue.Feldbreite
                            End With
                        End If
                        Grafikengine.Sprite.Draw2D(Symbole(I), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
                        Grafikengine.Sprite.Draw2D(SymbolMasken(I), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, Objektverwalter.Spielfeld.AktuellerSpieler.Farbe)
                    Else
                        With Grafikengine.recBild
                            .X = 0 : .Width = BreiteeinesSymbols
                            .Y = 0 : .Height = BreiteeinesSymbols
                        End With
                        Grafikengine.recBild.Width = Hauptmenue.FeldbreiteKlein
                        Grafikengine.recBild.Height = Hauptmenue.FeldbreiteKlein
                        Grafikengine.Sprite.Draw2D(Symbole(I), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
                    End If
                Else
                    With Grafikengine.recBild
                        .X = 0 : .Width = BreiteeinesSymbols
                        .Y = 0 : .Height = BreiteeinesSymbols
                    End With
                    Grafikengine.recBild.Width = Hauptmenue.FeldbreiteKlein
                    Grafikengine.recBild.Height = Hauptmenue.FeldbreiteKlein
                    Grafikengine.Sprite.Draw2D(Symbole(I), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
                End If
                Grafikengine.ptfPosition.Y += Hauptmenue.Feldbreite
            Next
            If Hauptmenue.ZoomedIn Then
                Text.neuerText(S, MenueRechteckGross.X + Hauptmenue.Feldbreite / 4 + MenueRechteckGross.Width * I2 + BreiteeinesSymbols, MenueRechteckGross.Y + Hauptmenue.Feldbreite / 2 + 1 + Mitte - 6, E, , 6)
            Else
                Text.neuerText(S, MenueRechteckGross.X + Hauptmenue.Feldbreite / 4 + MenueRechteckGross.Width * I2 + BreiteeinesSymbols, MenueRechteckGross.Y + Hauptmenue.Feldbreite / 2 + 1 + Mitte, E, , 3)
            End If

            Schriftengine.DrawText(Text, New Point)
        Next

        'Cursor Malen
        intColor = Color.FromArgb(255, 255, 255, 255)
        With Grafikengine.recBild
            .X = 18 * Frame : .Width = 18
            .Y = 0 : .Height = 16
        End With
        With Grafikengine.szfScale
            .Height = 16
            .Width = 18
        End With
        Grafikengine.ptfPosition = New Drawing.Point(MenueRechteckGross.X + MenueRechteckGross.Width * (Cursorposition.X + 1) - 10, MenueRechteckGross.Y + Hauptmenue.Feldbreite / 2 + 1 + Cursorposition.Y * Hauptmenue.Feldbreite)
        Grafikengine.Sprite.Draw2D(Cursor, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        Frame = 1
        'Grafikengine.Grafikkarte.EndScene()
        ' Grafikengine.Grafikkarte.BeginScene()
        Return True
    End Function
    Public Function CheckMouseDown() As String
        Return Tags(GetCursorPosition)
    End Function
    Public Sub MouseMove(ByVal X As Integer, ByVal Y As Integer)
        Dim Cursorpositionjetzt As Integer = GetCursorPosition()
        Dim R As Integer
        X -= MenueRechteckGross.X
        Y -= MenueRechteckGross.Y
        If X - 3 <= 0 Then
            Cursorposition.X = 0
        ElseIf X > MenueRechteckGross.Width * Spaltenzahl Then
            Cursorposition.X = Spaltenzahl - 1
        Else
            Cursorposition.X = Math.DivRem(X, MenueRechteckGross.Width, R)
        End If
        Dim Max As Integer = (Cursorposition.X + 1) * ItemZahl
        If Max > Tags.Length - 1 Then Max = Tags.Length
        Dim Min As Integer = Cursorposition.X * ItemZahl
        If Y - 5 <= 0 Then
            Cursorposition.Y = 0
        ElseIf Y > (Max - Min) * Hauptmenue.Feldbreite - Hauptmenue.Feldbreite / 2 Then
            Cursorposition.Y = Max - Min - 1
        Else
            Y -= 5
            Cursorposition.Y = Math.DivRem(Y, Hauptmenue.Feldbreite, 0)
        End If

        If Not GetCursorPosition() = Cursorpositionjetzt Then
            MusikEngine.LoadWav(DataSettings.Resourcenpfad + "Sound\Sound Effects\Menuewechsel.wav", False)
        End If
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="VeraenderungY">+1 oder -1</param>
    ''' <remarks></remarks>
    Public Sub CursorpositionVeraendern(ByVal VeraenderungY As Integer, ByVal VeraenderungX As Integer)
        If VeraenderungY > 0 Then
            Cursorposition.Y += 1
        ElseIf VeraenderungY < 0 Then
            Cursorposition.Y -= 1
        End If

        If VeraenderungX > 0 Then
            Cursorposition.X += 1
        ElseIf VeraenderungX < 0 Then
            Cursorposition.X -= 1
        End If

        If Cursorposition.Y < 0 Then
            Cursorposition.X -= 1
            Cursorposition.Y = ItemZahl - 1
        ElseIf Cursorposition.Y > ItemZahl - 1 Then
            Cursorposition.Y = 0
            Cursorposition.X += 1
        End If

        If Cursorposition.X < 0 Then
            Cursorposition.X = 0
            Cursorposition.Y = 0
        ElseIf Cursorposition.X > Spaltenzahl - 1 Then
            Cursorposition.X = Spaltenzahl - 1
            Cursorposition.Y = ItemZahl - 1
        End If
        Dim X As Integer = MenueRechteckGross.X + MenueRechteckGross.Width * Cursorposition.X
        Dim Y As Integer = MenueRechteckGross.Y + 5 + Cursorposition.Y * Hauptmenue.Feldbreite
        Hauptmenue.CursorPlazieren(X - Objektverwalter.Spielfeld.BildschirmKooX, Y - Objektverwalter.Spielfeld.BildschirmKooY, True)
        MusikEngine.LoadWav(DataSettings.Resourcenpfad + "Sound\Sound Effects\Menuewechsel.wav", False)
    End Sub
    ''' <summary>
    ''' Immediatlx realease the unmanaged Ressources used by the Engine
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Dispose()
        For I = 0 To Texturen.Length - 1
            Texturen(I).Dispose()
        Next
        ReDim Texturen(-1)
        Grafikengine.Grafikkarte.EvictManagedResources()
    End Sub
    Public Function GetCursorPosition() As Integer
        Return (Cursorposition.X) * ItemZahl + Cursorposition.Y
    End Function
End Class
